package com.example.ssmlab02.controller;
import com.example.ssm.model.Book;
import com.example.ssm.service.BookService;
import com.example.ssm.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
@RequestMapping(value = "book")
public class BookController extends BaseController{

    private BookService bookService;

    public BookController(BookService bookService) {
        this.bookService = bookService;
    }


    @RequestMapping(value = "addBook",method = RequestMethod.POST)
    private String addBook(Book book){
        bookService.create(book);
        List<Book> books = bookService.queryAll();
        session.setAttribute("books",books);
        return "redirect:/book/index.jsp";
    }

    @RequestMapping(value = "deleteById/{id}",method = RequestMethod.GET)
    private String deleteById(@PathVariable("id") int id){
        bookService.remove(id);
        session.setAttribute("books",bookService.queryAll());
        return "redirect:/book/index.jsp";
    }

    @RequestMapping(value = "detailById/{id}",method = RequestMethod.GET)
    private String detailById(@PathVariable("id") int id){
        Book book = bookService.queryByID(id);
        request.setAttribute("book",book);
        return "/book/detailBook.jsp";
    }

    @RequestMapping(value = "alterById/{id}",method = RequestMethod.GET)
    private String alterById(@PathVariable("id") int id){
        request.setAttribute("book",bookService.queryByID(id));
        return "/book/alterBook.jsp";
    }

    @RequestMapping(value = "updateById",method = RequestMethod.POST)
    private String updateById(Book book){
        bookService.modify("updateBookById",book);
        session.setAttribute("books",bookService.queryAll());
        return "redirect:/book/index.jsp";
    }
}